package cn.scut.srp.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class JWTUtils {

    @Value("${jwt.secret}")
    private String secret;

    @Value("${jwt.expiration}")
    private long expiration;

    public String generateToken(Integer userID,String userName, Integer userLevel) {
        Date now = new Date();
        Date expirationDate = new Date(now.getTime() + expiration * 1000);

        Key key = Keys.hmacShaKeyFor(secret.getBytes());

        Map<String, Object> claims = new HashMap<>();
        claims.put("sub", userID);
        claims.put("nme",userName);
        claims.put("lev",userLevel);
        claims.put("iat", now);
        claims.put("exp", expirationDate);

        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(expirationDate)
                .signWith(key)
                .compact();
    }

    public HashMap<String, Object> analyzeToken(String token) throws ExpiredJwtException {
        Key key = Keys.hmacShaKeyFor(secret.getBytes());
        Claims claims = Jwts.parserBuilder()
                .setSigningKey(key)
                .build()
                .parseClaimsJws(token)
                .getBody();

        String studentAccount = claims.getSubject();

        String pwd = (String)claims.get("pwd");
        Long exp = ((Double) claims.get("exp")).longValue();//过期时间
        Object nme = claims.get("nme");

        HashMap<String, Object> resultMap = new HashMap<>();

        resultMap.put("studentAccount", studentAccount);
        resultMap.put("password", pwd);
        resultMap.put("expireTime", exp);
        resultMap.put("studentName",nme);
        return resultMap;

    }

    public String generateToken(String studentAccount, String studentName, String password) {
        Date now = new Date();
        Date expirationDate = new Date(now.getTime() + expiration * 1000);

        Key key = Keys.hmacShaKeyFor(secret.getBytes());

        Map<String, Object> claims = new HashMap<>();
        claims.put("sub", studentAccount);
        claims.put("nme",studentName);
        claims.put("pwd",password);
        claims.put("iat", now);
        claims.put("exp", expirationDate);

        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(expirationDate)
                .signWith(key)
                .compact();
    }
    public String generateTeacherToken(String teacherAccount, String teacherName, String password) {
        Date now = new Date();
        Date expirationDate = new Date(now.getTime() + expiration * 1000);
        Key key = Keys.hmacShaKeyFor(secret.getBytes());

        Map<String, Object> claims = new HashMap<>();
        claims.put("sub", teacherAccount);
        claims.put("nme",teacherName);
        claims.put("pwd",password);
        claims.put("iat", now);
        claims.put("exp", expirationDate);

        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(expirationDate)
                .signWith(key)
                .compact();
    }
}
